﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jayden.Dll.Cards.Poker;
using System.IO;

namespace Jayden.Console.PokerServer
{
	public class ConsolePokerServer : Jayden.Dll.Cards.Poker.PokerServer
	{
		public ConsolePokerServer() : this(PokerProtocol.DefaultPort) { }
		public ConsolePokerServer(int port) : base(port) { }

		public override void OnServerMessage(string message)
		{
			System.Console.WriteLine("SERVER:> {0}", message);
		}

		public override void OnClientMessage(PokerClientThread client, string message)
		{
			System.Console.WriteLine("{0}:> {1}", client, message);
		}

		public override PokerPlayer LoadPlayer(string login, string password, PokerSocket socket)
		{
			string filename = "Accounts/" + login + ".txt";
			if (File.Exists(filename))
			{
				string[] data = File.ReadAllLines(filename);
				if(data.Length < 2)
					return null;
				if (data[0] != password)
					return null;
				ulong bankroll = ulong.Parse(data[1]);
				PokerPlayer player = PokerPlayer.GetPlayer(login, password, bankroll, socket);
				player.BankRoll = bankroll;
				return player;
			}
			return null;
		}

		public override void SavePlayer(PokerPlayer player)
		{
			string filename = "Accounts/" + player.Login + ".txt";
			string[] data = new string[]
			{
				player.Password,
				player.BankRoll.ToString(),
				player.Currency.IsoCode,
				DateTime.Now.ToString()
			};
			File.WriteAllLines(filename, data);
		}
	}
}
